﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Estimate;
using Newtonsoft.Json;

namespace Estimate.Reports
{
    /// <summary>
    /// 预报员预报与预报产品对照数据接口
    /// </summary>
    public class ForeCompareForeReport
    {
        FullInfoReport ForecasterReportScore;// 预报员得分对象
        Dictionary<string,FullInfoReport> ForeProductReportScores=new Dictionary<string,FullInfoReport>();//预报产品得分对象

        ExecutionStatus exeStatus;//程序执行状态
        ReportResult Result = new ReportResult();
        protected string ConnectionString;
        //string Cities = "";
        string Forecaster = "";
        string Station = "";

        List<string> ForeProducts = null;

        public ForeCompareForeReport(string connectionString, string forecaster, List<string> foreProducts,  string station, string tid)
        {

            ConnectionString = connectionString;
            Forecaster = forecaster;

            ForeProducts = foreProducts;
            Station = station;

            Dictionary<string, string> Stations = null;

            if (station != null && station != "")
            {
                Stations = new Dictionary<string, string>();
                Stations.Add(station, station);
            }


            if (tid != "")
                exeStatus = new ExecutionStatus(tid);

            ForecasterReportScore = new FullInfoReport(ConnectionString, null, Stations);
            ForecasterReportScore.CulculateAllResult = true;
            ForecasterReportScore.CulculateDeviationResult = false;
            ForecasterReportScore.ExeStatus = exeStatus;

            foreach (string foreProduct in ForeProducts)
            {
                FullInfoReport ForeProductReportScore = new FullInfoReport(ConnectionString, null, Stations);
                ForeProductReportScore.CulculateAllResult = true;
                ForeProductReportScore.CulculateDeviationResult = false;
                ForeProductReportScore.ExeStatus = exeStatus;

                ForeProductReportScores.Add(foreProduct,ForeProductReportScore);
            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ConnectionString"></param>
        /// <param name="tableName">数据表类型</param>
        /// <param name="tid">程序执行任务号</param>
        public ForeCompareForeReport(string connectionString, string forecaster, List<string> foreProducts,  string station, string tid, bool collectRelatedSemi)
            : this(connectionString, forecaster, foreProducts, station, tid)
        {
            if (collectRelatedSemi)
            {
                ForecasterReportScore.CulculateAllResult = false;

                foreach (string foreProduct in ForeProducts)
                    ForeProductReportScores[foreProduct].CollectForeRealSemi = true;

            }
           
        }

        /// <summary>
        /// 数据准备并计算
        /// </summary>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="realtype">实况类型</param>
        /// <param name="waterSpans">天气时效</param>
        /// <param name="tempSpans">温度时效</param>
        /// <param name="sendpoints">发报时间</param>
        /// <param name="CollectOrFilterDateOfNotForeData">筛选没有预报数据的日期，默认为收集=false</param>
        public void PrepareData(DateTime beginDate, DateTime endDate, ForeSemiType semitype, ForeSpanType[] waterSpans, ForeSpanType[] tempSpans, ForeSendpointType[] sendpoints,bool FilterDateOfNotForeData=false)
        {
            try
            {
                if (exeStatus != null)
                {
                    exeStatus.TotalSteps = ForecasterReportScore.GetExeStatusTotalSteps(beginDate, endDate, semitype, waterSpans, tempSpans) * (1 + ForeProducts.Count);
                    exeStatus.SetMessage(Forecaster, "计算预报员" + Forecaster + "得分");
                }

                ForecasterReportScore.DataProvider = ForeRepositoryFactory.ForecasterRepository(ConnectionString, Forecaster);
                ForecasterReportScore.PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints);

                if (exeStatus != null)
                    exeStatus.DeleteMessage(Forecaster);
            }
            catch (Exception ex)
            {
                Result.success = false.ToString();
                Result.msg = ex.Message;
                if (exeStatus != null)
                {
                    exeStatus.DeleteMessage(Forecaster);
                    exeStatus.SetMessage("Error", "程序执行出错");
                }
            }

            foreach (string foreProduct in ForeProducts)
            {
                try
                {
                    if (exeStatus != null)
                        exeStatus.SetMessage(foreProduct, "计算预报产品" + foreProduct + "得分");

                    ForeProductReportScores[foreProduct].DataProvider = new ForeRepository(ConnectionString, foreProduct);

                    if (FilterDateOfNotForeData)
                    {
                        ForeProductReportScores[foreProduct].DateListOfNotHaveWaterData = ForecasterReportScore.DateListOfNotHaveWaterData;
                        ForeProductReportScores[foreProduct].DateListOfNotHaveWaterData1 = ForecasterReportScore.DateListOfNotHaveWaterData1;
                        ForeProductReportScores[foreProduct].DateListOfNotHaveWaterData2 = ForecasterReportScore.DateListOfNotHaveWaterData2;
                        ForeProductReportScores[foreProduct].DateListOfNotHaveMaxTempData = ForecasterReportScore.DateListOfNotHaveMaxTempData;
                        ForeProductReportScores[foreProduct].DateListOfNotHaveMinTempData = ForecasterReportScore.DateListOfNotHaveMinTempData;
                        ForeProductReportScores[foreProduct].PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints, false);
                    }
                    else
                        ForeProductReportScores[foreProduct].PrepareData(beginDate, endDate, semitype, waterSpans, tempSpans, sendpoints, true);

                    if (exeStatus != null) exeStatus.DeleteMessage(foreProduct);
                }
                catch (Exception ex)
                {
                    Result.success = false.ToString();
                    Result.msg = ex.Message;
                    if (exeStatus != null)
                    {
                        exeStatus.DeleteMessage(foreProduct);
                        exeStatus.SetMessage("Error", "程序执行出错");
                    }
                }

            }
           
        }

        

        /// <summary>
        /// 获取核查表数据字符串
        /// </summary>
        /// <returns></returns>
        public string GetReportDataString(bool collectSemi=false)
        {
            if (exeStatus != null) exeStatus.SetMessage("GetReportDataString", "正在获得计算结果");

            ReportResult result = GetReportData(collectSemi);

            if (exeStatus != null)
            {
                exeStatus.DeleteMessage("GetReportDataString");
                exeStatus.SetMessage(true, "ExecutionEnd", "程序执行完毕");

            }

            return JsonConvert.SerializeObject(result);

        }

        public ReportResult GetReportData(bool collectSemi = false)
        {
            

            if (collectSemi)
            {
                Result.result.Add("semis", new Dictionary<string, object>());

                ((Dictionary<string, object>)Result.result["semis"]).Add(Forecaster, ForecasterReportScore.RelatedEntities);

                foreach (string foreProduct in ForeProducts)
                {
                    ((Dictionary<string, object>)Result.result["semis"]).Add(Forecaster, ForeProductReportScores[foreProduct].RelatedEntities);
                }

            }
            else
            {
                Result.result.Add("data", new Dictionary<string, object>());

                ((Dictionary<string, object>)Result.result["data"]).Add(Forecaster, ForecasterReportScore.ToResult(null, Station));

                foreach (string foreProduct in ForeProducts)
                {
                    ((Dictionary<string, object>)Result.result["data"]).Add(foreProduct, ForeProductReportScores[foreProduct].ToResult(null, Station));
                }
            }
            
            return Result;
        }

        public void Clear()
        {
            ForecasterReportScore.Clear();
            foreach (string foreProduct in ForeProducts)
                ForeProductReportScores[foreProduct].Clear();

            Result.success = true.ToString();
            Result.msg = "";
            Result.result = new Dictionary<string, object>();
        }
    }

    /// <summary>
    /// 预报员预报与预报产品对照数据
    /// </summary>
    public class ReportResult
    {
        public string success = true.ToString();
        public string msg = "";
        public Dictionary<string, object> result = new Dictionary<string, object>();

        public ReportResult()
        {
           
        }
    }
    
}
